Saturday, June 25, 2016

Telling your computer what to do

Telling your computer what to do

Some programming languages you might have heard of:

  • R
  • Matlab
  • Python
  • JavaScript
  • html

Interacting with your computer

Interacting with your computer

Interacting with your computer

Creating variables

baby <- 'Meredith'
sculpture <- 10
baby 
## [1] "Meredith"
sculpture
## [1] 10

What types of information does your computer understand?

  • numeric
    • integers, e.g. 1, 2, 3
    • doubles, e.g. 1.25, 3.78
    • complex numbers, e.g. 3 + 2i
  • character or string
    • e.g. "Research", "Platforms"
  • logical or Boolean
    • e.g. TRUE and FALSE

How can we group information?

  • vectors / tuples
  • matrices / data.frames / arrays
  • lists / structures
  • dictionaries

vectors / tuples

vectors / tuples

vector1 <- 1:10
vector2 <- c("learning", "to", "code", "is", "fun")
vector1
##  [1]  1  2  3  4  5  6  7  8  9 10
vector2
## [1] "learning" "to"       "code"     "is"       "fun"

matrices / data.frames / arrays

matrices / data.frames / arrays

PlatoonLeads <- data.frame(
  list(
    platoon = c("Data Wranglers", 
                "Data Miners", 
                "Data Vizards", 
                "Cadventurers"),
    name    = c("Kerry Halupka", 
                "Kim Doyle", 
                "Isabell Kiral-Kornek", 
                "Louise van der Werff")))
PlatoonLeads
##          platoon                 name
## 1 Data Wranglers        Kerry Halupka
## 2    Data Miners            Kim Doyle
## 3   Data Vizards Isabell Kiral-Kornek
## 4   Cadventurers Louise van der Werff

lists

lists

shoppingList <- list(
  breakfast = c("cereal", "milk", "orange juice", "banana"),
  lunch     = c("bread", "cheese", "tomato"),
  dinner    = c("frozen pizza", "chocolate mousse")
  )
shoppingList
## $breakfast
## [1] "cereal"       "milk"         "orange juice" "banana"      
## 
## $lunch
## [1] "bread"  "cheese" "tomato"
## 
## $dinner
## [1] "frozen pizza"     "chocolate mousse"

dictionaries

dictionaries

IMDBRatings = {"Game of Thrones": 9.4, 
    "Sherlock": 9.2, 
    "Firefly": 9.1, 
    "Friends": 8.9}

for movie, rating in IMDBRatings.items():
    print movie + ': ' + str(rating)
## Firefly: 9.1
## Friends: 8.9
## Sherlock: 9.2
## Game of Thrones: 9.4

How to add flexibility to the conversation

If statements

How to add flexibility to the conversation

If statements

hungry <- sample(c(TRUE, FALSE), 1)
if (hungry){
  print("GIVE ME A TIM TAM!!!")
} else {
  print("No tim tam for me, thanks!")
}
## [1] "No tim tam for me, thanks!"

How to add flexibility to the conversation

For loops

How to add flexibility to the conversation

For loops

for (beats in 1:4){
  print("bring left foot to right foot")
  print("step left foot back to original position")
  print("bring right foot to left foot")
  print("step left foot back to original position")
}
## [1] "bring left foot to right foot"
## [1] "step left foot back to original position"
## [1] "bring right foot to left foot"
## [1] "step left foot back to original position"
## [1] "bring left foot to right foot"
## [1] "step left foot back to original position"
## [1] "bring right foot to left foot"
## [1] "step left foot back to original position"
## [1] "bring left foot to right foot"
## [1] "step left foot back to original position"
## [1] "bring right foot to left foot"
## [1] "step left foot back to original position"
## [1] "bring left foot to right foot"
## [1] "step left foot back to original position"
## [1] "bring right foot to left foot"
## [1] "step left foot back to original position"

How to add flexibility to the conversation

Functions

How to add flexibility to the conversation

Functions

moodCalendar <- function(day){
  if (day %in% c("Friday", "Saturday", "Sunday")){
    print(paste(day, "is a happy day"))
  } else if (day %in% c("Monday", "Tuesday")){
    print(paste(day, "is a grumpy day"))
  } else {
    print(paste(day, "is a neutral day"))
  }
}
moodCalendar("Monday")
## [1] "Monday is a grumpy day"
moodCalendar("Thursday")
## [1] "Thursday is a neutral day"

Where to find help

Making mistakes is the best way to learn